package com.city.parking;

import com.baidu.navisdk.comapi.tts.BNTTSPlayer;
import com.baidu.navisdk.comapi.tts.BNavigatorTTSPlayer;
import com.baidu.navisdk.comapi.tts.IBNTTSPlayerListener;
import com.city.parking.util.SharePrefUtil;
import com.city.parking.util.Utils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
import com.umeng.update.UmengUpdateAgent;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends FragmentActivity {
	private DrawerLayout mDrawerLayout;
	private FragmentManager fm;
	private GdMapFragment gd;
	private BdMapFragment bd;
	private int curMap = ParkingApp.UNKNOWN_MAP;
	
	private static MainActivity instance = null;
	public FeedbackAgent fb = null;
	public static MainActivity getInstance() {
		return instance;
	}
	
	public Fragment getFragment() {
		if (ParkingApp.mMapType == ParkingApp.BAIDU_MAP) {
			if (bd == null)
				bd =new BdMapFragment();
			return bd;
		} else {
			if (gd == null)
				gd =new GdMapFragment();
			return gd;
		}
	}
	
    public void setUpUmengFeedback() {
        fb = new FeedbackAgent(this);
        // check if the app developer has replied to the feedback or not.
        fb.sync();
        fb.openAudioFeedback();
        fb.openFeedbackPush();
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean result = fb.updateUserInfo();
            }
        }).start();
    }

    public void setUpUmengPush() {
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        PushAgent.getInstance(this).onAppStart();
        String device_token = UmengRegistrar.getRegistrationId(this);
        Utils.log("device_token="+device_token);
    }

    private void setMap() {
    	if (curMap == ParkingApp.mMapType)
    		return;
    	
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.map_frame, getFragment(), null);
		ft.addToBackStack(null);
		ft.commit();

		if (ParkingApp.mMapType == ParkingApp.BAIDU_MAP)
			mDrawerLayout.setDrawerListener(bd);
		else
			mDrawerLayout.setDrawerListener(gd);
		
		curMap = ParkingApp.mMapType;
    }
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);
		
		fm = getSupportFragmentManager();
		
		// 初始化TTS. 开发者也可以使用独立TTS模块，不用使用导航SDK提供的TTS
		BNTTSPlayer.initPlayer();
		//设置TTS播放回调
		BNavigatorTTSPlayer.setTTSPlayerListener(new IBNTTSPlayerListener() {
            
            @Override
            public int playTTSText(String arg0, int arg1) {
            	//开发者可以使用其他TTS的API
                return BNTTSPlayer.playTTSText(arg0, arg1);
            }
            
            @Override
            public void phoneHangUp() {
                //手机挂断
            }
            
            @Override
            public void phoneCalling() {
                //通话中
            }
            
            @Override
            public int getTTSState() {
            	//开发者可以使用其他TTS的API,
                return BNTTSPlayer.getTTSState();
            }
        });

        MobclickAgent.setDebugMode(true);
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.updateOnlineConfig(this);
        UmengUpdateAgent.setUpdateOnlyWifi(true);
        UmengUpdateAgent.setDeltaUpdate(true);

        if (SharePrefUtil.getAutoUpdateInWifi()) {
        	UmengUpdateAgent.silentUpdate(this);
        } else {
            UmengUpdateAgent.update(this);
        }
        
        setUpUmengFeedback();
        setUpUmengPush();
	}

	@Override
	public void onBackPressed() {
        new AlertDialog.Builder(this)
        .setTitle("请确认")
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setMessage("要退出程序吗？")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                MainActivity.this.finish();
            }
        }).setNegativeButton("取消", null).show();
	};

	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    BNTTSPlayer.stopTTS();
	};

	@Override
	protected void onResume() {
	    // TODO Auto-generated method stub
		setMap();
		
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
	    // TODO Auto-generated method stub
	    super.onPause();
	    MobclickAgent.onPause(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void openSideMenu() {
		mDrawerLayout.openDrawer(Gravity.LEFT);
	}
}
